package com.qcxy.gzy.activity.v2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.qcxy.gzy.activity.R;
import com.qcxy.gzy.activity.base.HiddenAdaptiveActivity;

import java.util.Timer;
import java.util.TimerTask;

public class TotalActivity extends HiddenAdaptiveActivity implements OnClickListener, OnSeekBarChangeListener, OnTouchListener {

    protected SeekBar mSeekBar;
    protected TextView mTargetValue;
    protected TextView mTargetNumber;
    protected TextView mUnit;

    protected int mTarget = 0;
    protected Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_v2_total);

        findViewById(R.id.iv_back).setOnClickListener(this);
        mSeekBar = findViewById(R.id.sb_target);
        mTargetValue = findViewById(R.id.tv_target);
        mTargetNumber = findViewById(R.id.tv_target_number);
        mUnit = findViewById(R.id.tv_target_unit);
        findViewById(R.id.iv_target_reduce).setOnClickListener(this);
        findViewById(R.id.iv_target_plus).setOnClickListener(this);
        findViewById(R.id.iv_target_reduce).setOnTouchListener(this);
        findViewById(R.id.iv_target_plus).setOnTouchListener(this);
        findViewById(R.id.ll_go).setOnClickListener(this);
        mSeekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mTarget = progress;
        mTargetValue.setText(String.valueOf(progress));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    public class TimeTask extends TimerTask {
        private int type;

        public TimeTask(int type) {
            this.type = type;
        }

        @Override
        public void run() {
            Message message = new Message();
            if (type == 1) {
                message.what = type;
            } else if (type == 2) {
                message.what = type;
            } else if (type == 3) {
                message.what = type;
            } else {
                message.what = type;
            }
            new CustomHandler(Looper.getMainLooper()).sendMessage(message);
        }
    }

    class CustomHandler extends Handler {
        CustomHandler(Looper looper) {
            super(looper);
        }

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (mTarget > 0) {
                        mTarget--;
                        mTargetValue.setText(String.valueOf(mTarget));
                        mSeekBar.setProgress(mTarget);
                    }
                    break;
                case 2:
                    if (mTarget < 100) {
                        mTarget++;
                        mTargetValue.setText(String.valueOf(mTarget));
                        mSeekBar.setProgress(mTarget);
                    }
                    break;
                case 3:
                    if (mTarget < 60) {
                        mTarget++;
                        mTargetValue.setText(String.valueOf(mTarget));
                        mSeekBar.setProgress(mTarget);
                    }
                    break;
                case 4:
                    if (mTarget < 200) {
                        mTarget++;
                        mTargetValue.setText(String.valueOf(mTarget));
                        mSeekBar.setProgress(mTarget);
                    }
                    break;
            }
        }
    }
}